package javase.chixing.thread_synchronized;

class BankObject {
    private double balance;//余额

    //存款
    public synchronized void deposit(double money){
        this.setBalance(this.getBalance()+money);
        System.out.println(Thread.currentThread().getName()+"存款结束余额为:"+this.getBalance());
        //这边可以让每个线程执行结束之后sleep等待2毫秒
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //取款
    public synchronized void withdraw(double money){
        this.setBalance(this.getBalance()-money);
        System.out.println(Thread.currentThread().getName()+">>>取款结束余额为<<<:"+this.getBalance());
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}
